package com.liang.spring.test.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import sun.text.resources.FormatData;

import com.liang.spring.test.dao.BadbookingInfoMapper;
import com.liang.spring.test.controller.entity.BadbookingInfo;
import com.liang.spring.test.controller.entity.BadbookingInfoExample;
import com.liang.spring.test.controller.entity.UserInfo;
import com.liang.spring.test.controller.entity.BadbookingInfoExample.Criteria;

@Controller
@RequestMapping(value = "/badbooking")
public class badbookingController {

	@Autowired
	private BadbookingInfoMapper badbookingInfoMapper;

	@RequestMapping(value = "/selBadbookinginfo")
	public String selBadbookinginfo(String fid, Model model){
		try {		
			BadbookingInfoExample example = new BadbookingInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andFidEqualTo(Integer.parseInt(fid));
			List<BadbookingInfo> badbookingList = badbookingInfoMapper.selectByExample(example);
			model.addAttribute("userid", fid);
			model.addAttribute("badbookingList", badbookingList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/selPersonalcredit";
	}
	
	@RequestMapping(value = "/add")
	public String selBadbookinginfo(BadbookingInfo badbooking){
		try {		
			BadbookingInfoExample example = new BadbookingInfoExample();
			int count = badbookingInfoMapper.selCountByid(badbooking.getFid()) + 1;
			badbooking.setFcount(count);
			badbookingInfoMapper.insert(badbooking);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "redirect:selBadbookinginfo?fid=" + badbooking.getFid();
	}
	
	@RequestMapping(value = "/selBadrecord")
	public String selAll(Model model, HttpSession session){
		String msg = "";
		try {		
			BadbookingInfoExample example = new BadbookingInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andFidEqualTo(((UserInfo) session.getAttribute("user")).getFid());
			session.removeAttribute("");
			int count = badbookingInfoMapper.selCountByid(((UserInfo) session.getAttribute("user")).getFid());
			if (count == 0) {
				BadbookingInfo badinfo = new BadbookingInfo();
				badinfo.setFcount(0);
				session.setAttribute("userBadrecord", badinfo);
			} else if (count == 1) {
				criteria.andFcountEqualTo(1);
				List<BadbookingInfo> badbookingList = badbookingInfoMapper.selectByExample(example);
				BadbookingInfo badinfo = badbookingList.get(0);
				session.setAttribute("userBadrecord", badinfo);
				msg = "尊敬的乘客，您有一次违规订票记录，下次订票时会有一定的限制！";
			} else if (count == 2) {
				criteria.andFcountEqualTo(2);
				List<BadbookingInfo> badbookingList = badbookingInfoMapper.selectByExample(example);
				BadbookingInfo badinfo = badbookingList.get(0);
				session.setAttribute("userBadrecord", badinfo);
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				msg = "尊敬的乘客，您有两次违规订票记录，在\t" + simpleDateFormat.format(badinfo.getFlocktodate()) + "之前只能订购【硬卧】座位！";
			}
			model.addAttribute("msg", msg);
			model.addAttribute("count", count);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/seltrain";
	}
}
